
/*
 * CommonController.java
 *
 * Created on 2006
 */

package com.qbooks.common;

import java.io.*;


import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

import com.zll.framework.conf.Configuration;
import com.zll.framework.servlet.Command;
import com.zll.framework.util.StringUtil;




/**
 *
 * @author eastar
 * @version
 */
public class RestController extends HttpServlet {

    /**
	 *
	 */
	private static final long serialVersionUID = 1L;

	/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
    	String nextURL = "";

    	/*
		 * RESTful URI 목록
		 * 
		 * 1. 출판사책목록 		www.qbooks.co.kr/[출판사ID]
		 * 2. 출판사책정보		www.qbooks.co.kr/[출판사ID]/[책일련번호]
		 * 3. 책정보ISBN			www.qbooks.co.kr/isbn/[ISBN번호]
		 * 4. 책정보ecn			www.qbooks.co.kr/ecn/[ECN번호]	
    	 */
    	String path = request.getServletPath();
    	

    	System.out.println(path);
    	
    	
    	String[] dirs = path.split("/");
    	
    	for (int i = 0; i < dirs.length; i++) {
    		System.out.println(i+":"+dirs[i]);
    	}
    	
    	if (dirs.length == 2) {
    		request.setAttribute("book_code", dirs[1]+dirs[2]);
			nextURL = "/home/store/bookDetail.jsp";  
    	}
    	else if (dirs.length == 3) {
    		if (dirs[1].equals("isbn")) {
        		request.setAttribute("isbn", dirs[2]);
    			nextURL = "/home/store/bookDetail.jsp";
    		}
    		else if (dirs[1].equals("ecn")) {
        		request.setAttribute("ecn", dirs[2]);
    			nextURL = "/home/store/bookDetail.jsp";    			
    		}
    		else if (dirs[1].equals("publisher")) {
    			nextURL = "/store/bookList.jsp?ty=publisher&publisher_id="+dirs[2];
    		}
    		else {
        		
    		}
    	}


    	System.out.println("\n\n\n nextURL:" + nextURL);


//    	try
//    	{
//    		Command command = (Command)Class.forName(Configuration.getPackageName() + cmd + "Command").newInstance();
//		    nextURL = command.execute(request, response);
//	   	}
//	   	catch (Exception e) {
//	   		e.printStackTrace();
//	   		request.setAttribute("exception", e);
//	   		nextURL = "/common/error_page.jsp";
//	   	}
    	
    	if ("".equals(nextURL)) {
    		request.setAttribute("exception", new Exception("존재하지 않는 페이지입니다."));
	   		nextURL = "/common/error_page.jsp";
    	}

   		RequestDispatcher rd = getServletContext().getRequestDispatcher(nextURL);
		rd.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
